package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.MchntApplyInfoMapper;
import cc.rengu.igas.bomp.common.entity.MchntApplyInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/6
 */
public class MchntApplyInfoMapperImpl implements MchntApplyInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertMchntApplyInfo(MchntApplyInfo mchntApplyInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMchntApplyInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntApplyInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户申请信息失败,instId:<{}>,mchntNname:<{}>,contactNname:<{}>,contactPhone:<{}>,applyId:<{}>,iReturnCode:<{}>",
                    mchntApplyInfo.getInstId(), mchntApplyInfo.getMchntName(), mchntApplyInfo.getContactName(), mchntApplyInfo.getContactPhone(), mchntApplyInfo.getApplyId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateMchntApplyInfoById(MchntApplyInfo mchntApplyInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntApplyInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntApplyInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户申请信息失败,instId:<{}>,mchntNname:<{}>,contactNname:<{}>,contactPhone:<{}>,applyId:<{}>,iReturnCode:<{}>",
                    mchntApplyInfo.getInstId(), mchntApplyInfo.getMchntName(), mchntApplyInfo.getContactName(), mchntApplyInfo.getContactPhone(), mchntApplyInfo.getApplyId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public MchntApplyInfo selectMchntApplyInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntApplyInfoById");
        MchntApplyInfo mchntApplyInfo = new MchntApplyInfo();
        mchntApplyInfo.setInstId(instId);
        mchntApplyInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntApplyInfo, mchntApplyInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户申请信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return mchntApplyInfo;
    }

    @Override
    public MchntApplyInfo selectMchntApplyInfoByPrimaryKey(String instId, String applyId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntApplyInfoByPrimaryKey");
        MchntApplyInfo mchntApplyInfo = new MchntApplyInfo();
        mchntApplyInfo.setInstId(instId);
        mchntApplyInfo.setApplyId(applyId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntApplyInfo, mchntApplyInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户申请信息失败,instId:<{}>,applyId:<{}>,iReturnCode:<{}>", instId, applyId, iReturnCode);
            return null;
        }
        return mchntApplyInfo;
    }

    @Override
    public List<MchntApplyInfo> selectMchntApplyInfoByMchntName(String instId, String mchntNname) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntApplyInfoByMchntName");
        MchntApplyInfo mchntApplyInfo = new MchntApplyInfo();
        mchntApplyInfo.setInstId(instId);
        mchntApplyInfo.setMchntName(mchntNname);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntApplyInfo, MchntApplyInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户申请信息失败,instId:<{}>,mchntName:<{}>", instId, mchntNname);
            return null;
        }
        return objectList.stream().map(item -> (MchntApplyInfo) item).collect(Collectors.toList());
    }
}
